<?php

namespace App\Http\Controllers\Api;

use App\Models\Author;
use App\Models\Book;
use Illuminate\Http\Request;

class AuthorController extends BaseController
{
    public function index(Request $request)
    {
        $page      = intval($request->get('page',1));
        $page      = max($page ,1);
        $page_size = intval($request->get('page_size',10));

        $columns = ['id','author_name','list_image'];
        $list = Author::query()->where('deleted',0)->orderBy('sort','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $author_name = preg_replace('/\[(.*?)\]/', '', $v->author_name);
                $like = '%'.trim($author_name).'%';
                $books = Book::query()->select(['id','title'])->where('author','like',$like)->where('deleted',0)->where('state',1)->orderBy('total_stock','desc')->limit(3)->get();

                $items[] = [
                    'id'          => $v->id,
                    'author_name' => $v->author_name,
                    'list_image'  => $v->list_image,
                    'books'       => $books
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items"     => $items,
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function show(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $info = Author::find($id);
        if(empty($info))
        {
            return $this->err(1,200,'','作者不存在');
        }

        $columns = ['*'];

        $author_name = preg_replace('/\[(.*?)\]/', '', $info->author_name);
        $like = '%'.trim($author_name).'%';
        $items = Book::query()->select($columns)->where('author','like',$like)->where('state',1)->where('deleted',0)->orderBy('total_stock','desc')->get();


        $books = [];
        if($items->isNotEmpty())
        {
            foreach ($items as $item)
            {
                $stock_arr = [];
                $discount_arr = [];
                $price_arr = [];

                $discount_arr2 = [];
                $price_arr2 = [];
                if($item->new_book_stock > 0)
                {
                    $stock_arr[] = $item->new_book_stock;
                    $discount_arr2[] = $item->new_book_discount;
                    $price_arr2[] = $item->new_book_price;
                }
                else
                {
                    $discount_arr[] = $item->new_book_discount;
                    $price_arr[] = $item->new_book_price;
                }

                if($item->good_book_stock > 0)
                {
                    $stock_arr[] = $item->good_book_stock;
                    $discount_arr2[] = $item->good_book_discount;
                    $price_arr2[] = $item->good_book_price;
                }
                else
                {
                    $discount_arr[] = $item->good_book_discount;
                    $price_arr[] = $item->good_book_price;
                }

                if($item->medium_book_stock > 0)
                {
                    $stock_arr[] = $item->medium_book_stock;
                    $discount_arr2[] = $item->medium_book_discount;
                    $price_arr2[] = $item->medium_book_price;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount;
                    $price_arr[] = $item->medium_book_price;
                }

                if($item->medium_book_stock2 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock2;
                    $discount_arr2[] = $item->medium_book_discount2;
                    $price_arr2[] = $item->medium_book_price2;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount2;
                    $price_arr[] = $item->medium_book_price2;
                }

                if($item->medium_book_stock3 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock3;
                    $discount_arr2[] = $item->medium_book_discount3;
                    $price_arr2[] = $item->medium_book_price3;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount3;
                    $price_arr[] = $item->medium_book_price3;
                }

                if($item->medium_book_stock4 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock4;
                    $discount_arr2[] = $item->medium_book_discount4;
                    $price_arr2[] = $item->medium_book_price4;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount4;
                    $price_arr[] = $item->medium_book_price4;
                }

                if($item->medium_book_stock5 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock5;
                    $discount_arr2[] = $item->medium_book_discount5;
                    $price_arr2[] = $item->medium_book_price5;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount5;
                    $price_arr[] = $item->medium_book_price5;
                }

                if($item->medium_book_stock6 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock6;
                    $discount_arr2[] = $item->medium_book_discount6;
                    $price_arr2[] = $item->medium_book_price6;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount6;
                    $price_arr[] = $item->medium_book_price6;
                }

                $min_discount = 0;
                $min_price = 0.0;
                $is_more = 0;
                if(count($stock_arr) > 1)
                {
                    $is_more = 1;
                }

                if(count($stock_arr) == 0)
                {
                    if(!empty($discount_arr))
                    {
                        $min_discount = min($discount_arr);
                    }
                    if(!empty($price_arr))
                    {
                        $min_price = min($price_arr);
                    }
                }
                else
                {
                    if(!empty($discount_arr2))
                    {
                        $min_discount = min($discount_arr2);
                    }
                    if(!empty($price_arr2))
                    {
                        $min_price = min($price_arr2);
                    }
                }

                $books[] = [
                    'id'            => $item->id,
                    'title'         => $item->title,
                    'author'        => $item->author,
                    'score'         => $item->score,
                    'image'         => $item->image,
                    'old_price'     => $item->old_price,
                    'is_more'       => $is_more,
                    'min_discount'  => $min_discount,
                    'min_price'     => $min_price,
                    'total_stock'   => $item->total_stock,
                ];
            }
        }
        $result = [
            'id'           => $info->id,
            'author_name'  => $info->author_name,
            'country'      => $info->country,
            'author_image' => $info->author_image,
            'intro'        => $info->intro,
            'books'        => $books
        ];
        return $this->ok($result);
    }
}
